import openpyxl
import os

def tomcat_log_collector(log_path):
    projects = os.listdir(log_path)
    project_dict = {}
    for project in projects:
        project_list = []
        project_dir = os.path.join(log_path, project)
        project_logs = os.listdir(project_dir)
        for project_log in project_logs:
            if 'tomcat' in project_log:
                project_log_path = os.path.join(project_dir, project_log)
                project_list.append(project_log_path)
        project_dict[project] = project_list
    copy_dict = project_dict.copy()
    for project in copy_dict:
        if not project_dict[project]:
            project_dict.pop(project)

    # print(len(project_dict))
    return project_dict

def parse_tomcat_log(log_path):
    with open(log_path, 'r', encoding='utf-8') as f:
        lines = f.readlines()
    data = {}
    keywords = ['服务器版本', 'cpu核心数', 'cpu使用率', '内存容量/内存使用率', '磁盘使用率', '系统日志', '内核参数',
                '防火墙配置', '服务器总连接数', '服务器连接信息', 'tomcat进程状态', 'tomcat进程信息', 'Tomcat版本信息',
                'Java版本信息', '进程jvm配置信息', '进程ulimit配置', '进程开机启动配置', 'Tomcat连接数', 'Tomcat连接信息',
                'Tomcat配置文件列表', 'Tomcat配置文件内容:', '异常日志:']
    for index in range(len(keywords)):
        start_keyword = keywords[index]
        end_keyword = ''
        if index != len(keywords) - 1:
            end_keyword = keywords[index + 1]
        print(start_keyword, '---', end_keyword)




if __name__ == '__main__':
    log_path = r'C:\Users\lixiu\OneDrive\桌面\2024第三季度'
    project_dict = tomcat_log_collector(log_path)
    for project in project_dict:
        if project_dict[project]:
            for log_file in project_dict[project]:
                print(log_file)
                parse_tomcat_log(log_file)

